package com.jianguo.web.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jianguo.common.core.domain.AjaxResult;
import com.jianguo.common.core.page.TableDataInfo;
import com.jianguo.user.domain.PzUser;
import com.jianguo.user.domain.PzUserAddress;
import com.jianguo.user.domain.UserInfo;
import com.jianguo.user.service.IPzBalanceService;
import com.jianguo.user.service.IPzUserAddressService;
import com.jianguo.user.service.IPzUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xjunjie
 * @date 2024/03/12
 */
@RestController
@RequestMapping("/api/userAddress")
public class UserAddressApi {
    @Autowired
    private IPzUserAddressService pzUserAddressService;

    @Autowired
    private IPzUserService pzUserService;


    @GetMapping("/list")
    public AjaxResult list(String account)
    {
        PzUser pzUser = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, account));
        List<PzUserAddress> list = pzUserAddressService.list(new LambdaQueryWrapper<PzUserAddress>()
                .eq(PzUserAddress::getUserId, pzUser.getId()));
        return AjaxResult.success(list);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody PzUserAddress address)
    {
        PzUser pzUser = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, address.getUserId()));
        if (!ObjectUtil.isEmpty(pzUser)){
            address.setUserId(pzUser.getId()); //账号转成userid，前端没有
        }
        address.setDetail(address.getProvince() + "," + address.getDetail());
        pzUserAddressService.save(address);
        return AjaxResult.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable String id)
    {
        return AjaxResult.success(pzUserAddressService.removeById(id));
    }

}
